package cn.edu.xmist.aolaitesi.controller;

import cn.edu.xmist.aolaitesi.config.Vars;
import cn.edu.xmist.aolaitesi.entity.HousePO;
import cn.edu.xmist.aolaitesi.entity.ProductPO;
import cn.edu.xmist.aolaitesi.service.HouseService;
import cn.edu.xmist.aolaitesi.service.ProductService;
import cn.edu.xmist.aolaitesi.util.ObjtoLayJson;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
//对商品进行添加修改与删除,查询等
@RestController
@RequestMapping("/house")
public class HouseController {

  public static final String[] COLUMNS = {"id", "houseCode", "houseName"};

  @Autowired private HouseService houseService;
  @Autowired private ProductService wareService;

  @RequestMapping("/list")
  public String list(Integer page, Integer limit) throws Exception {
    List<HousePO> list = houseService.list();

    return ObjtoLayJson.ListtoJson(list, COLUMNS);
  }

  @RequestMapping("/saveOrUpdate")
  public String saveOrUpdate(HousePO house) {
    if (house.getId() == null) {
      QueryWrapper<HousePO> queryWrapper = new QueryWrapper<>();
      queryWrapper.eq("house_code", house.getHouseCode());
      List<HousePO> list = houseService.list(queryWrapper);
      if (list.size() > 0) {
        return Vars.FAILURE;
      }
    }
    Boolean flag = houseService.saveOrUpdate(house);
    return Vars.SUCCESS;
  }

  @RequestMapping("/deleteById")
  public String deleteById(Long id) {
    HousePO house = houseService.getById(id);
    QueryWrapper<ProductPO> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("house_code", house.getHouseCode());
    List<ProductPO> list = wareService.list(queryWrapper);
    if (list.size() > 0) {
      return Vars.FAILURE;
    }
    Boolean flag = houseService.removeById(id);
    return Vars.SUCCESS;
  }
}
